﻿using System;
using Dapper;
using IntelligentGateway.Model;

namespace IntelligentGateway.Data
{
    public class UserDA
    {
        public UserInfo? LoginProc(string LoginCode, string Password)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    UserInfo? info = conn.QueryFirstOrDefault<UserInfo>("SELECT ID,UserName,LoginCode FROM userinfo Where LoginCode=@LoginCode and Password=@Password", new { LoginCode = LoginCode, Password = Password});
                    return info;
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
        public bool UpdatePassword(string strUser, string strNewPassword)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    int count = conn.Execute("update userinfo Set Password = @Password Where LoginCode =@LoginCode", new { Password = strNewPassword, LoginCode = strUser });
                    return count > 0;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
        //public bool Insert(Model.UserInfo User)
        //{
        //    string strSql = ("INSERT INTO `UserInfo`(UserName,LoginCode,Password,ProjectID) VALUES (@UserName,@LoginCode,@Password,@ProjectID);");
        //    using (var conn = DataFactory.GetMYSQLConnection())
        //    {
        //        try
        //        {
        //            int count = conn.Execute(strSql, User);
        //            return count > 0;
        //        }
        //        catch (Exception e)
        //        {
        //            return false;
        //        }
        //    }
        //}
    }
}
